<?php

namespace App\Controller;

use App\Constants\AdRequestEventTypeEnum;
use App\Model\Business\Application;
use App\Model\Business\ChannelCode;
use App\Model\Report\BigDevice;
use App\Model\Report\ReportAdRequest;
use App\Model\Report\ReportCpm;
use App\Request\Report\ReportAdRequestReq;
use App\Request\Report\ReportBehaviorReq;
use App\Services\Impl\CommonService;
use App\Services\Impl\ReportService;
use Carbon\Carbon;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;

class ReportController extends AbstractController
{
    #[Inject]
    protected CommonService $commonService;

    #[Inject]
    protected ReportService $reportService;

    /**
     * 广告请求上报
     * @param ReportAdRequestReq $req
     * @return ResponseInterface
     */
    public function adRequest(ReportAdRequestReq $req): ResponseInterface
    {
        $params    = $req->all();
        $appId     = $params['appId'];
        $channel   = $params['channel'];
        $oaid      = $params['oaid'];
        $eventType = $params['eventType'];

        $app = Application::findOrFail($appId);

        $ip = $this->commonService->getClientIp();

        $channelType = ChannelCode::findChannelTypeByChannel($channel);

        $bigDevice          = BigDevice::findDevice($appId, $oaid);
        $ascribeChannelType = $bigDevice->channel_type ?? 0;
        $ascribeChannel     = $bigDevice->channel ?? '';
        $planId             = $bigDevice->plan_id ?? '';
        $planName           = $bigDevice->plan_name ?? '';
        $adId               = $bigDevice->ad_id ?? '';
        $adName             = $bigDevice->ad_name ?? '';

        ReportAdRequest::create([
            'project_id'             => $app->project_id,
            'app_id'                 => $appId,
            'channel_type'           => $channelType,
            'channel'                => $params['channel'],
            'version'                => $params['version'],
            'ascribe_channel_type'   => $ascribeChannelType,
            'ascribe_channel'        => $ascribeChannel,
            'oaid'                   => $oaid,
            'ip'                     => $ip,
            'cpm'                    => $params['cpm'],
            'session_id'             => $params['sessionId'],
            'request_id'             => $params['requestId'],
            'event_type'             => $eventType,
            'error_code'             => $params['errorCode'],
            'plan_id'                => $planId,
            'plan_name'              => $planName,
            'ad_id'                  => $adId,
            'ad_name'                => $adName,
            'ad_network_platform_id' => $params['adNetworkPlatformId'],
            'ad_position'            => $params['adPosition'],
            'date'                   => Carbon::now()->toDateString()
        ]);

        if ($eventType == AdRequestEventTypeEnum::SHOW->value) {
            ReportCpm::create([
                'project_id'             => $app->project_id,
                'app_id'                 => $appId,
                'channel_type'           => $channelType,
                'channel'                => $params['channel'],
                'version'                => $params['version'],
                'ascribe_channel_type'   => $ascribeChannelType,
                'ascribe_channel'        => $ascribeChannel,
                'oaid'                   => $params['oaid'],
                'ip'                     => $ip,
                'session_id'             => $params['sessionId'],
                'cpm'                    => $params['cpm'],
                'plan_id'                => $planId,
                'plan_name'              => $planName,
                'ad_id'                  => $adId,
                'ad_name'                => $adName,
                'ad_network_platform_id' => $params['adNetworkPlatformId'],
                'ad_position'            => $params['adPosition'],
                'date'                   => Carbon::now()->toDateString()
            ]);
        }

        return $this->succeed();
    }

    public function behavior(ReportBehaviorReq $req): ResponseInterface
    {
        $params = $req->all();
        $appId = $params['appId'];
        $channel = $params['channel'];
        $version = $params['version'];
        $oaid = $params['oaid'];
        $eventType = $params['eventType'];

        $this->reportService->reportEvent($appId, $channel, $version, $oaid, $eventType, '');

        return $this->succeed();
    }
}
